<template>
  <div class="main">
    <div class="one">
      角色：
      <el-select v-model="value1" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.id"
          :label="item.rolename_zh"
          :value="item.rolename_zh"
        >
        </el-option>
      </el-select>
      <el-button type="success" @click="lookrole">查看现有角色</el-button>
    </div>
    <hr />
    <div class="a">
      <div class="two">
        <h3>菜单分配</h3>
        <el-button type="success" @click="addrole">赋予权限</el-button>
        <el-transfer v-model="value2" :data="data"></el-transfer>
      </div>
      <div class="three">
        <h3>目前菜单</h3>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <div id="text"></div>
            <el-button style="float: right; padding: 3px 0" type="text"
              >操作按钮</el-button
            >
          </div>
          <div v-for="item in menulist" :key="item.id" class="text item">
            {{ item.name }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            {{ item.id }}
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import { getRole } from "@/api/role.js";
import { getMenu } from "@/api/menu.js";
import { getMenubyRole } from "@/api/role";
import { addRoleToRole } from "@/api/role";
export default {
  data() {
    return {
      //左边框中的列表值
      data: [],
      //右边框中的值
      value2: [],

      options: [],
      value1: "",

      menulist: []
    };
  },
  created() {
    //角色
    getRole("/role/getRole").then(res => {
      if (res.code === 200) {
        this.options = res.data;
      } else if (res.code === 500) {
        this.$message(res.data);
      }
    });

    //菜单
    getMenu("/menu/getmenu").then(res => {
      if (res.code === 200) {
        this.data = res.data;
      } else if (res.code === 500) {
        this.$message(res.data);
      }
    });
  },
  methods: {
    lookrole() {
      var parent = document.getElementById("text");
      parent.innerHTML = this.value1 + "——角色拥有下面菜单";
      getMenubyRole("/role/getmenubyrole", { rolename: this.value1 }).then(
        res => {
          if (res.code === 200) {
            this.menulist = res.data;
          } else if (res.code === 500) {
            this.$message(res.data);
          }
        }
      );
    },

    //添加权限
    addrole() {
      addRoleToRole("/role/addmenutorole", {
        menu: this.value2,
        role: this.value1
      }).then(res => {
        if (res.code === 200) {
          this.lookrole();
          this.$message("权限添加成功");
          this.value2 = "";
        } else if (res.code === 500) {
          this.$message(res.data);
        }
      });
    }
  }
};
</script>

<style scoped>
.main {
  width: 100%;
  height: 680px;
  border: 1px solid red;
}
.one {
  margin-left: 400px;
  margin-top: 20px;
}
.two {
  width: 60%;
  margin-top: 20px;
  float: right;
}
.three {
  width: 29%;
  margin-left: 100px;
  margin-top: 20px;
  float: left;
}
</style>
